package com.chezai.client.utils;


import android.content.Context;
import android.content.SharedPreferences;

import com.chezai.client.global.Const;
import com.chezai.client.global.TerminalApplication;


public class PreferenceManager {
    private static final String SP_TERMINAL = "sp_terminal";
    private static Object serialNo;

    public static SharedPreferences getSharedPreferences() {
        return TerminalApplication.mContext.getSharedPreferences(SP_TERMINAL, Context.MODE_PRIVATE);
    }


    public static boolean isFirstTime(String key) {
        if (getBoolean(key, false)) {
            return false;
        } else {
            putBoolean(key, true);
            return true;
        }
    }

    public static void clear() {
        SharedPreferences.Editor edit = getSharedPreferences().edit();
        edit.clear();
        edit.commit();
    }


    public static boolean contains(String key) {
        return getSharedPreferences().contains(key);
    }

    public static int getInt(final String key, final int defaultValue) {
        return PreferenceManager.getSharedPreferences().getInt(key, defaultValue);
    }

    public static boolean putInt(final String key, final int pValue) {
        final SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putInt(key, pValue);
        return editor.commit();
    }

    public static long getLong(final String key, final long defaultValue) {
        return getSharedPreferences().getLong(key, defaultValue);
    }

    public static Long getLong(final String key, final Long defaultValue) {
        if (getSharedPreferences().contains(key)) {
            return getSharedPreferences().getLong(key, 0);
        } else {
            return null;
        }
    }


    public static boolean putLong(final String key, final long pValue) {
        final SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putLong(key, pValue);
        return editor.commit();
    }

    public static boolean putFloat(final String key, final float pValue) {
        final SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putFloat(key, pValue);
        return editor.commit();
    }

    public static float getFloat(final String key, final float pValue) {
        return getSharedPreferences().getFloat(key, pValue);
    }

    public static boolean getBoolean(final String key, final boolean defaultValue) {
        return getSharedPreferences().getBoolean(key, defaultValue);
    }

    public static boolean putBoolean(final String key, final boolean pValue) {
        final SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putBoolean(key, pValue);
        return editor.commit();
    }

    public static String getString(final String key, final String defaultValue) {
        return getSharedPreferences().getString(key, defaultValue);
    }

    public static boolean putString(final String key, final String pValue) {
        final SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.putString(key, pValue);
        return editor.commit();
    }


    public static boolean remove(final String key) {
        final SharedPreferences.Editor editor = getSharedPreferences().edit();
        editor.remove(key);
        return editor.commit();
    }

    public static boolean isRegister() {
        return getBoolean(Const.SP_IS_TERMINAL_REGISTER, false);
    }

    public static int getSerialNo() {
        int serialNo = PreferenceManager.getInt(Const.SP_SERIAL_NO, 0) + 1;
        PreferenceManager.putInt(Const.SP_SERIAL_NO, serialNo);
        return serialNo;
    }
}